package com.hanlin.backend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanlin.backend.common.entity.HlRole;
import com.hanlin.backend.dto.RoleDto;
import com.hanlin.backend.dto.UserInfo;
import com.hanlin.backend.mapper.HlRoleMapper;
import com.hanlin.backend.service.HlRoleService;
import com.hanlin.backend.service.HlUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author hl.yuan
 * @since 2021-01-06
 */
@Service
public class HlRoleServiceImpl extends ServiceImpl<HlRoleMapper, HlRole> implements HlRoleService {


    @Autowired
    private HlUserService hlUserService;

    @Override
    public List<RoleDto> getRoleByUserId(int userId) {
        return this.getBaseMapper().getRoleByUserId(userId);
    }

    @Override
    public IPage<RoleDto> getTableRoleList(RoleDto dto) {
        IPage page = dto.buildPage();
        return getBaseMapper().getTableRoleList(page, dto);
    }

    @Override
    @Cacheable(value = "roleList", key = "'roleList:'+#roleId+'_'+#roleName")
    public List<RoleDto> getRoleList(Integer roleId, String roleName) {
        return getBaseMapper().getRoleList(roleId, roleName);
    }

    @Override
    public List<Map> getRoleAndUser() {
        // 获取角色列表
        List<RoleDto> list = getRoleList(null,null);
        List<Map> listMap = new ArrayList<>();
        for (RoleDto dto : list) {
            Map map = new HashMap();
            map.put("label",dto.getRoleName());
            map.put("roleId",dto.getRoleId());
            List<Map> userByRoleId = hlUserService.getUserByRoleId(dto.getRoleId());
            map.put("children",userByRoleId);
            listMap.add(map);
        }

        return listMap;
    }
}
